/**
  ******************************************************************************
  * @file    AT25DF041.h
  * @author  Thongnh
  * @version V1.0
  * @date    18-07-2011
  * @brief   Main process of Flash AT25DF041
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, XICUTELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 Xicutelectronics</center></h2>
  ******************************************************************************
  */ 

#ifndef __AT25DF041_H__
#define __AT25DF041_H__

#include "BasicTypes.h"
#include "BoardHardware.h"


class cFlash
{
public:
    // FLASH SPI opcodes
    static const U8 READ_LOW_SPEED         = 0x03;    // Read data from memory array beginning at selected address
    static const U8 READ_HIGH_SPEED        = 0x0B;    // Read data from memory array beginning at selected address
    static const U8 BLOCK_ERASE_4K         = 0x20;
    static const U8 BLOCK_ERASE_32K        = 0x52;
    static const U8 BLOCK_ERASE_64K        = 0xD8;
    static const U8 CHIP_ERASE1            = 0x60;
    static const U8 CHIP_ERASE2            = 0xC7;
    static const U8 WRITE_1_256            = 0x02;
    static const U8 SEQUENTIAL_WRITE1      = 0xAD;
    static const U8 SEQUENTIAL_WRITE2      = 0xAF;
    static const U8 WRITE_ENABLE           = 0x06;
    static const U8 WRITE_DISABLE          = 0x04;
    static const U8 PROTECT_SECTOR         = 0x36;
    static const U8 UNPROTECT_SECTOR       = 0x39;
    //static const U8 GLOBAL_PROTECT         =         // user write status register command
    //static const U8 GLOBAL_UNPROTECT       =         // user write status register command
    static const U8 READ_SECTOR_PROTECT    = 0x3C;
    static const U8 READ_STATUS            = 0x05;
    static const U8 WRITE_STATUS           = 0x01;
    static const U8 READ_DEVICE_ID         = 0x9F;    //read device ID of chip
    static const U8 DEEP_POWER_DOWN        = 0xB9;
    static const U8 RESUM_DEEP_POWER_DOWN  = 0xAB;

    static const U8 GLOBAL_PROTECT_CODE    = 0x3C;
    static const U8 GLOBAL_UNPROTECT_CODE  = 0x00;

private:
    static void CsLow();
    static void CsHigh();
    static U8 SendByte(U8 byte);
    static U8 ReadStatus();
    static void GlobalProtect();
    static void GlobalUnprotect();
    static bool IsBusy();
    static bool IsWriteDisable();

public:
    static void InitReset();
    static void ReadDeviceID(U8 *pBuff);
    static U8 ReadByte(U32 addr);
    static bool Read(U32 addr, U8 *pBuff, U16 length);
    static bool WriteByte(U32 addr, U8 iData);
    static bool Write(U32 addr, U8 * pBuff, U16 length);
    static bool EraseBlock(U8 cmd, U32 addr);
    static bool EraseChip();
};

#endif
// end of file

